<html>
<head>
<script>
    function print(message, color) 
    {
        var paragraph = document.createElement("div");
        paragraph.appendChild(document.createTextNode(message));
        paragraph.style.fontFamily = "monospace";
        if (color)
            paragraph.style.color = color;
            document.getElementById("console").appendChild(paragraph);
    }

    function shouldBe(a, b)
    {
        var evalA = eval(a);
        if (evalA == b) 
            print("PASS: " + a + " should be " + b + " and is.", "green");
        else {
            print("FAIL: " + a + " should be " + b + " but instead is " + evalA + ".", "red");
            numErrors ++;
        }
    }
    
    function step() {
        switch (state) {
            case 0:
            shouldBe('window.history.length == originalHistoryLength', true);
            shouldBe('window.location.hash', '');
            window.location.hash = 'foo';
            shouldBe('window.location.hash', '#foo');
            shouldBe("window.location == originalLocation + '#foo'", true);
            shouldBe('window.history.length ==  originalHistoryLength + 1', true);
            break;
        case 1:
            window.location.hash = 'bar';
            shouldBe('window.location.hash', '#bar');
            shouldBe("window.location == originalLocation + '#bar'", true);
            shouldBe('window.history.length == originalHistoryLength + 2', true);
            break;
        case 2:
            window.location.hash = 'bar';
            shouldBe('window.location.hash', '#bar');
            shouldBe("window.location == originalLocation + '#bar'", true);
            shouldBe('window.history.length == originalHistoryLength + 2', true);
            break;        
        case 3:
            window.history.back();
            shouldBe('window.location.hash', '#foo');
            shouldBe("window.location == originalLocation + '#foo'", true);
            break;
        case 4:
            window.history.back();
            shouldBe('window.location.hash', '');
            shouldBe("window.location == originalLocation", true);
            break;
        case 5:
            window.history.forward();
            shouldBe('window.location.hash', '#foo');
            shouldBe("window.location == originalLocation + '#foo'", true);
            break;
        case 6:
            window.location.hash = '';
            if (numErrors == 0)
                print("SUCCESS!", "green")
            else
                print("FAILURE: one or more tests failed", "red");
                
            if (window.layoutTestController)
                layoutTestController.notifyDone();
                
            return;
        }
        
        state ++;
        step();
    }
        
    function runTests() {
        if (window.layoutTestController) {
            layoutTestController.clearBackForwardList();
            layoutTestController.dumpAsText();
            layoutTestController.waitUntilDone();
        }
        
        state = 0;
        numErrors = 0;
        originalLocation = window.location.href;
        originalHistoryLength = window.history.length;
     
        step();
    }
    </script>
</head>
<body onload="runTests();">
<p>This tests that modifying location.hash works as it should</p>
<div id="console">
</div>
</body>
</html>
